//
//  PRTPlayer.m
//  Pirates
//
//  Created by goofara on 4/4/14.
//  Copyright (c) 2014 Training Camp. All rights reserved.
//

#import "PRTPlayer.h"

#define kPositionX 0
#define kPositionY 0
#define kRotation 0
#define kPiOver180 M_PI/180.0f

@implementation PRTPlayer

- (id)init
{
    return [self initWithPosition:CGPointMake(kPositionX, kPositionY) rotation:kRotation andSprite:nil];
}

-(id)initWithPosition:(CGPoint)position rotation:(float)rotation andSprite:(CCSprite *)sprite
{
    self = [super init];
    if(self)
    {
        self.playerPosition = position;
        self.playerRotation = rotation;
        self.playerImage = sprite;
    }
    return self;
}

-(CGPoint) move
{
    self.playerRotation = -(self.playerImage.rotation+180);
    CGPoint newPosition = CGPointMake(self.playerPosition.x + (cos((self.playerRotation - 90) * kPiOver180) * self.speed),  self.playerPosition.y + (sin((self.playerRotation - 90) * kPiOver180))*self.speed);
    
    CGPoint newPositionWithOffset = CGPointMake(newPosition.x - self.playerOffsetFromOrigin.x, newPosition.y - self.playerOffsetFromOrigin.y);
    if([self.delegate canMoveToTileCoordinateForPosition:newPositionWithOffset])
    {
        self.playerPosition = newPosition;
        
    }
    return self.playerPosition;
}

-(void)steerWithDirectionalAngle:(CGFloat)angle
{
    if(angle>0){
        [self.playerImage runAction:[CCActionRotateBy actionWithDuration:angle/90 angle:(angle*-1)/2]];
    }
    else{
        [self.playerImage runAction:[CCActionRotateBy actionWithDuration:(angle/90)*-1 angle:(angle*-1)/2]];
    }
//    CCActionRotateBy * rotation = [CCActionRotateBy actionWithDuration:0.05f angle:angle];
//    [self.playerImage runAction:rotation];
//    self.playerRotation += angle;
    
}
-(void)increaseSpeed
{
    if(self.speed == 0)
    {
        self.speed = self.playerStats.maximumSpeed * 0.5f;
    }
    else
    {
        self.speed = self.playerStats.maximumSpeed;
    }
}

-(void)decreaseSpeed
{
    if(self.speed == self.playerStats.maximumSpeed)
    {
        self.speed = self.playerStats.maximumSpeed * 0.5f;
    }
    else
    {
        self.speed = 0;
    }
}

@end
